<?php
class products_model extends MY_model
{
    public $id;
    public $name;
    public $unit;
    public $price;

    public function get_elements()
    {
        $query = $this->db->query("SELECT * FROM products");
        return $this->to_domain_models($query->result());
    }

    public function get_element_by_id($id)
    {
        $query = $this->db->query("SELECT * FROM products WHERE id = ?",array($id));
        return $this->to_domain_model($query->row());
    }

    public function search_product($keyword)
    {
        $query = $this->db->query("SELECT * FROM products WHERE name LIKE ?",array('%' . $keyword . '%'));
        return $this->to_domain_models($query->result());
    }

    // -------------------------------------------------------------------------------- PRIVATE PROPERTIES

    public function get_formatPrice()
    {
        return number_format($this->price);
    }

    public function get_totalPrice()
    {
        return number_format($this->price);
    }

    // -------------------------------------------------------------------------------- PRIVATE METHOD

    // Private method
    public function to_domain_model($item)
    {
        $model = new products_model();
        $model->id = isset($item->id) ? $item->id : '' ;
        $model->name = isset($item->name) ? $item->name : '' ;
        $model->unit = isset($item->unit) ? $item->unit : '' ;
        $model->price = isset($item->price) ? $item->price : '0' ;

        return $model;
    }
}